package com.fmyd.qizhuazhua.http;

import com.loopj.android.http.RequestParams;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * Created by xyh
 */
public class HttpHelper {

    /**
     * 将map转换成url
     *
     * @param params
     * @return
     */
    protected String spellUrlParamsByMap(Map<String, Object> params) {
        if (params == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = s.substring(0, s.lastIndexOf("&"));
        }
        return s;
    }

    /**
     * 将map转换成 RequestParams
     *
     * @param map
     * @return
     */
    protected RequestParams getParamsByMap(Map<String, Object> map) {
        RequestParams params = new RequestParams();
        if (map == null) {
            return params;
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            params.put(entry.getKey(), entry.getValue());
        }
        return params;
    }

    /**
     * 将Obj对象转化为RequestParams
     *
     * @param obj
     * @return
     */
    public static RequestParams toRequestParams(Object obj) {
        // 获取f对象对应类中的所有属性域
        RequestParams params = new RequestParams();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (int i = 0, len = fields.length; i < len; i++) {
            // 对于每个属性，获取属性名
            String varName = fields[i].getName();
            try {
                // 获取原来的访问控制权限
                boolean accessFlag = fields[i].isAccessible();
                // 修改访问控制权限
                fields[i].setAccessible(true);
                // 获取在对象f中属性fields[i]对应的对象中的变量
                Object value = fields[i].get(obj);
                if (null == value) {
                    value = "";
                }
                params.put(varName, value);
                fields[i].setAccessible(accessFlag);
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
        return params;
    }

    /**
     * 将obj转换成url
     *
     * @param obj
     * @return
     */
    public static String spellUrlParamsByObj(Object obj) {
        if (obj == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (int i = 0, len = fields.length; i < len; i++) {
            String varName = fields[i].getName();
            try {
                boolean accessFlag = fields[i].isAccessible();
                fields[i].setAccessible(true);
                Object value = fields[i].get(obj);
                if (null == value) {
                    value = "";
                }
                sb.append("&");
                sb.append(varName + "=" + value);
                fields[i].setAccessible(accessFlag);
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
        return sb.toString();
    }
}
